﻿/*----------------------------------------------------------------
//  <copyright file="InjectMiniProfiler.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2022-11-15 15:51</last-date>
//----------------------------------------------------------------*/

using StackExchange.Profiling;

namespace MicroCloud.Swagger.SwaggerUI
{
    #region "注入MiniProfiler"
    /// <summary>
    /// 注入MiniProfiler
    /// </summary>
    public class InjectMiniProfiler : IDocumentFilter
    {
        private readonly IHttpContextAccessor _httpContext;

        /// <summary>
        /// 初始化一个 注入MiniProfiler 的新实例
        /// </summary>
        /// <param name="httpContext"></param>
        public InjectMiniProfiler(IHttpContextAccessor httpContext)
        {
            _httpContext = httpContext;
        }

        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="swaggerDoc"></param>
        /// <param name="context"></param>
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var scriptString = StackExchange.Profiling.MiniProfiler.Current.RenderIncludes(_httpContext.HttpContext).ToString();
            swaggerDoc.Info.Contact = new()
            {
                Name = scriptString
            };
        }
    }
    #endregion

}
